<?php
	/**
	 * @author Matthias Eigner | 0625203
	 */
	class Message{
		private $id;
		private $fId;
		private $kind;
		private $author;
		private $title;
		private $text;
		private $date;
		private $replys;
		private $reply_count;
		private $allMsgCount;
		
		const MESSAGE_KIND_USER = 'u';
		const MESSAGE_KIND_LIST = 'l';
		const MESSAGE_KIND_MESSAGE = 'm';
		const MESSAGE_KIND_TODO = 't';
		
		function __construct($id, $author, $title, $text, $date, $r, $lID=-1, $mID=-1, $uID=-1, $tID=-1){
			$this->id=$id;
			
			$this->fId=($lID+$mID+$uID+$tID)+3;
			$this->kind = isset($uID) ? self::MESSAGE_KIND_USER :
						(isset($lID) ? self::MESSAGE_KIND_LIST :
						(isset($mID) ? self::MESSAGE_KIND_MESSAGE : 
						(isset($tID) ? self::MESSAGE_KIND_TODO : '')));
			$this->title = $title;
			$this->author = $author;
			$this->text = $text;
			$this->date = $date;
			$this->replys = array();
			$this->reply_count = count($r);

			if(count($r) > 0 && isset($r[0]['author'])){
				foreach($r as $re){
					$lID = isset($re['lID']) ? $re['lID'] : -1;
					$uID = isset($re['uID']) ? $re['uID'] : -1;
					$tID = isset($re['tID']) ? $re['tID'] : -1;
					$mID = isset($re['mID']) ? $re['mID'] : -1;
					$rep = isset($re['r']) ? $re['r'] : array();
					$this->replys[] = new Message($re['id'], new UserDescription($re['author']['nick'], $re['author']['id'], $re['author']['fN'], $re['author']['lN'], '', 0), $re['title'],  $re['text'],  $re['date'], $rep, $lID, $mID, $uID, $tID);
				}
			}
		}
		
		/**
		 * 
		 * Set all Message count for Paging and output 
		 * @param int $allMsgCount
		 */
		public function setAllMsgCount($allMsgCount){
			$this->allMsgCount = $allMsgCount;
		}
		
		//Getters
		public function getId(){
			return $this->id;
		}
		
		public function getFId() {
			return $this->fId;
		}
		
		public function getKind() {
			return $this->kind;
		}
		
		public function getAuthor(){
			return $this->author;
		}
		
		public function getTitle() {
			return htmlspecialchars($this->title);
		}
		
		public function getText() {
			return htmlspecialchars($this->text);
		}
		
		public function getDate() {
			return  date('d.m.Y', $this->date);
		}
		
		public function getReplys() {
			return $this->replys;
		}
		
		public function getReplyCount() {
			return $this->reply_count;
		}
		
		public function getAllMsgCount() {
			return $this->allMsgCount;
		}
		
		
		/**
		 * 
		 * Renders Message and all Replys -depr
		 */
		/*public function render() {
			$replace = new ViewDescriptor('content/parts/message');
			$replace->addValue('id', $this->id);
			$replace->addValue('title', $this->getTitle());
			$replace->addValue('text', $this->getText());
			$replace->addValue('date', $this->getDate());
			$replace->addValue('a_id', $this->getAuthor()->getId());
			$replace->addValue('a_name', $this->getAuthor()->getName());
			$r = '';
			foreach($this->getReplys() as $re){
				$r .= $re->render();
			}
			$replace->addValue('replys', $r);
			
			return $replace->render();
		}*/
	}
?>